import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Properties;

public class GetConnection {
    public void getConnection() throws Exception{
        //读取配置文件的基本信息
        InputStream in = GetConnection.class.getClassLoader().getResourceAsStream("jdbc.properties");

        Properties pros = new Properties();
        pros.load(in);

        String user = pros.getProperty("user");
        String password = pros.getProperty("password");
        String url = pros.getProperty("url");
        String driverClass = pros.getProperty("driverClass");

        //加载驱动
        Class.forName(driverClass);

        //获取连接
        Connection conn = DriverManager.getConnection(url, user, password);

        //预编译sql语句，返回PreparedStatement
        String sql = "insert into hotsearch(id,dt,content) values(?,?,?)";
        PreparedStatement ps = conn.prepareStatement(sql);

        //对占位符进行填充
        ps.setString(1, "32");
        ps.setString(2, "2020-1-11");
        ps.setString(3, "哈哈");

        //执行操作
        ps.execute();

        //关闭资源
        ps.close();
        conn.close();
    }

    public static void main(String[] args) {
        try {
            new GetConnection().getConnection();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
